<template>
  <div class="contianer">
    <div class="header">
      <el-button icon="el-icon-plus" @click="handleOpenDialog(false, 1)"
        >新增菜品分类</el-button
      >
      <el-button icon="el-icon-plus" @click="handleOpenDialog(false, 2)"
        >新增套餐分类</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="分类名称"> </el-table-column>
      <el-table-column label="分类类型">
        <template v-slot="{ row }">
          {{ row.type === 1 ? "菜品分类" : "套餐分类" }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="操作时间"> </el-table-column>
      <el-table-column prop="sort" label="排序"></el-table-column>
      <el-table-column label="操作" v-slot="{ row }">
        <el-link type="primary" @click="handleOpenDialog(true, row.type, row)"
          >修改</el-link
        >
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary">删除</el-link>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="`${isEdit ? '编辑' : '新增'}${
        form.type == 1 ? '菜品' : '套餐'
      }分类`"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model.number="form.sort"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCategory, changeType, addCategory } from "@/api/category";
let form = {
  type: "1",
  name: "",
  sort: "",
};
export default {
  data() {
    return {
      input: "",
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      isEdit: false,
      form: JSON.parse(JSON.stringify(form)),
    };
  },
  created() {
    this.init();
  },
  methods: {
    handleOpenDialog(isEdit, type, row) {
      this.isEdit = isEdit;
      if (this.isEdit) {
        this.form = JSON.parse(JSON.stringify(row));
      } else {
        this.form = JSON.parse(JSON.stringify(form));
      }
      this.form.type = type;
      this.dialogVisible = true;
    },
    async handleSubmit() {
      if (this.isEdit) {
        await changeType(this.form);
        this.$message.success("编辑成功");
      } else {
        await addCategory(this.form);
        this.$message.success("新增成功");
      }
      this.dialogVisible = false;
      this.init();
    },
    init() {
      getCategory(this.pageInfo).then((res) => {
        const { records, total } = res.data;
        this.tableData = records;
        this.total = total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
  },
};
</script>

<style lang="scss" scoped>
.contianer {
  padding: 33px;
  .header {
    display: flex;
    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }
  }
  .footer {
    margin-top: 22px;
    display: flex;
    justify-content: center;
  }
}
</style>
